package com.thesis.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.AnswerService;
import com.thesis.manage.service.StudentTeacherService;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/2/24
 */
@RestController
@RequestMapping("/answer")
public class AnswerController extends BaseController {

    @Resource
    private AnswerService answerService;

    @Resource
    private StudentTeacherService studentTeacherService;

    @GetMapping("/list")
    public Map<String, Object> list(Pageable pageable) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            String collegeName = teacher.getCollegeName();
            return answerService.list(collegeName, pageable);
        });
    }

    /**
     * 导入分组(分配学生)
     *
     * @param file 分组信息文件
     */
    @PostMapping("/import")
    public Map<String, Object> importGroupInfo(MultipartFile file) {
        return buildMessage(() -> {
            answerService.importGroupInfo(file);
            return null;
        });
    }

    /**
     * 通过教师编号查询分组学生
     *
     * @param teacherNumber 教师编号
     */
    @GetMapping("/{teacherNumber}/groupInfo")
    public Map<String, Object> findGroupInfoByTeacherNumber(@PathVariable String teacherNumber) {
        return buildMessage(() -> answerService.findGroupInfoByTeacherNumber(teacherNumber));
    }

    @PostMapping("/add")
    public Map<String, Object> add(String studentNumber, String teacherNumber) {
        return buildMessage(() -> {
            answerService.add(studentNumber, teacherNumber);
            return null;
        });
    }

    @DeleteMapping("/{id}/delete")
    public Map<String, Object> delete(@PathVariable Long id) {
        return buildMessage(() -> {
            studentTeacherService.delete(id);
            return null;
        });
    }
}
